﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KhuyenMaiVang.DAL;
using KhuyenMaiVang.Areas.Admin.Models;
using KhuyenMaiVang.Helpers;

namespace KhuyenMaiVang.Areas.Admin.Controllers
{ 
    public class UserController : Controller
    {
        private KhuyenMaiVangDataContext db = new KhuyenMaiVangDataContext();

        //
        // GET: /Admin/User/

        public ViewResult Index()
        {
            var users = db.Users.Include("Group");
            ViewBag.TempDataSuccess = (String)TempData["success"];
            return View(users.ToList());
        }

        //
        // GET: /Admin/User/Details/5
        
        public ViewResult Details(int id)
        {
            User user = db.Users.Single(u => u.ID == id);
            return View(user);
        }

        //
        // GET: /Admin/User/Create

        public ActionResult Create()
        {
            ViewBag.GroupID = new SelectList(db.Groups, "ID", "Name", 3);
            return View();
        } 

        //
        // POST: /Admin/User/Create

        [HttpPost]
        public ActionResult Create(ResUser user)
        {
            ViewBag.TempDataError = (String)TempData["error"];
            if (ModelState.IsValid)
            {
                if (user.Password == user.PasswordConfirm)
                {
                    if (!ProcessHelper.checkUsernameOrEmail(0, user.Username, user.Email))
                    {
                        User curUser = ProcessHelper.resUserToUser(user);

                        curUser.Password = ProcessHelper.md5AddSecret(user.Password);
                        curUser.Status = 1;
                        curUser.CreatedTime = DateTime.Now;

                        db.Users.AddObject(curUser);
                        db.SaveChanges();
                        TempData["success"] = "Đã thêm thành viên:" + user.Username;
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Đã có người sử dụng user hoặc email");
                        ViewBag.GroupID = new SelectList(db.Groups, "ID", "Name", user.GroupID);
                        return View(user);
                    }
                }
                else
                {
                    ModelState.AddModelError("","Xác nhận mật khẩu không đúng");
                    ViewBag.GroupID = new SelectList(db.Groups, "ID", "Name", user.GroupID);
                    return View(user);
                }
            }

            ViewBag.GroupID = new SelectList(db.Groups, "ID", "Name", user.GroupID);
            return View(user);
        }
        
        //
        // GET: /Admin/User/Edit/5
        public ActionResult Edit(int id)
        {
            Request.ValidateInput();
            User user = db.Users.Single(u => u.ID == id);

            ViewBag.GroupID = new SelectList(db.Groups, "ID", "Name", user.GroupID);
            ResUser rUser = ProcessHelper.userToResUser(user);
            return View(rUser);
        }

        //
        // POST: /Admin/User/Edit/5
        [HttpPost]
        public ActionResult Edit(ResUser rUser)
        {
            if (ModelState.IsValid)
            {
                if (rUser.Password == rUser.PasswordConfirm)
                {
                    if (!ProcessHelper.checkUsernameOrEmail(rUser.ID,  rUser.Username, rUser.Email))
                    {
                        User user = ProcessHelper.resUserToUser(rUser);
                        user.Password = ProcessHelper.md5AddSecret(rUser.Password);
                        db.Users.Attach(user);
                        db.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
                        db.SaveChanges();
                        TempData["success"] = "Đã sửa thành viên:" + user.Username;
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Tên đăng nhập hoặc email đã tồn tại");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Xác nhận mật khẩu không đúng");
                }
            }
            ViewBag.GroupID = new SelectList(db.Groups, "ID", "Name", rUser.GroupID);
            return View(rUser);
        }

        //
        // GET: /Admin/User/Delete/5
 
        public ActionResult Delete(int id)
        {
            User user = db.Users.Single(u => u.ID == id);
            return View(user);
        }

        //
        // POST: /Admin/User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            User user = db.Users.Single(u => u.ID == id);
            db.Users.DeleteObject(user);
            db.SaveChanges();
            TempData["success"] = "Đã xóa thành viên:" + user.Username;
            return RedirectToAction("Index");
        }
        //
        // GET: /Admin/User/Delete/5

        public ActionResult Change(int id)
        {
            User user = db.Users.Single(u => u.ID == id);
            return View(user);
        }

        //
        // POST: /Admin/User/Delete/5

        [HttpPost, ActionName("Change")]
        public ActionResult ChangeConfirmed(int id)
        {
            User user = db.Users.Single(u => u.ID == id);
            if (user.Status == 1)
            {
                user.Status = 0;
            }
            else
            {
                user.Status = 1;
            }
            db.SaveChanges();
            TempData["success"] = "Đã thay đổi trạng thái thành viên:" + user.Username;
            return RedirectToAction("Index");
        }
        public ActionResult Order()
        {
            var lstOrder = new List<Order>();
            for (int i = 0; i < 10; i++)
            {
                Order order = new Order();
            }
                return View();
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}